import sys
import re

def count_characters(text):
    return len(text)

def count_words(text):
    words = re.findall(r"[A-Za-z]+", text)
    return len(words)

def count_sentences(text):
    sentences = re.findall(r"[\.!?]", text)
    return len(sentences)

def main():
    # 参数数量检查
    if len(sys.argv) != 3:
        print("Usage: wc.exe [-c | -w | -s] <filename>")
        sys.exit(1)

    option = sys.argv[1]
    filename = sys.argv[2]

    # 读取文件
    try:
        with open(filename, "r", encoding="utf-8") as f:
            text = f.read()
    except FileNotFoundError:
        print(f"Error: cannot open file '{filename}'")
        sys.exit(1)

    # 判断选项
    if option == "-c":
        result = count_characters(text)
        print(result)

    elif option == "-w":
        result = count_words(text)
        print(result)

    elif option == "-s":
        result = count_sentences(text)
        print(result)

    else:
        print("Invalid option. Use -c / -w / -s")
        sys.exit(1)


if __name__ == "__main__":
    main()
